/*
 *  Copyright 2009 Allan Lykke Christensen.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package dk.i2m.netbeans.modules.doxia;

import java.awt.Image;
import java.awt.Insets;
import java.io.Serializable;
import java.io.StringReader;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewDescription;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.awt.UndoRedo;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;

/**
 * Description of the APT view editor.
 *
 * @author Allan Lykke Christensen
 */
public class AptViewDescription extends JPanel implements MultiViewDescription,
        MultiViewElement, Serializable {

    private AptEditorSupport support;
    private JTextPane textPanel;
    private JScrollPane slider;

    public AptViewDescription(AptEditorSupport support) {
        this.support = support;
        initComponents();
    }

    private void initComponents() {
        textPanel = new JTextPane();
        textPanel.setContentType("text/html");
        textPanel.setEditable(false);
        textPanel.setMargin(new Insets(10, 10, 10, 10));
        slider = new JScrollPane(textPanel);
    }

    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ONLY_OPENED;
    }

    public String getDisplayName() {
        return "View";
    }

    public Image getIcon() {
        return null;
    }

    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
    }

    public String preferredID() {
        return "AptViewDescription";
    }

    private void renderTextPane() {
        HTMLEditorKit kit = (HTMLEditorKit) textPanel.getEditorKit();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
        doc.getStyleSheet().importStyleSheet(getClass().getResource(
                "/dk/i2m/netbeans/modules/doxia/rendered.css"));
        System.out.println(getClass().getResource("/dk/i2m/netbeans/modules/doxia/external.png"));

        try {
            String apt = support.getDataObject().getPrimaryFile().asText();
            String xhtml = AptUtils.aptToXhtml(apt);
            kit.read(new StringReader(xhtml), doc, 0);
            textPanel.setDocument(doc);
        } catch (Exception ex) {
            textPanel.setText(ex.getMessage());
            Exceptions.printStackTrace(ex);
        }
    }

    public MultiViewElement createElement() {
        return this;
    }

    public JComponent getVisualRepresentation() {
        return slider;
    }

    public JComponent getToolbarRepresentation() {
        return this;
    }

    public Action[] getActions() {
        return support.getDataObject().
                getNodeDelegate().getActions(false);
    }

    public Lookup getLookup() {
        return ((AptDataObject) support.getDataObject()).getNodeDelegate().
                getLookup();
    }

    public void componentOpened() {
    }

    public void componentClosed() {
    }

    /**
     * Event handler executed when the "View" is being shown.
     */
    public void componentShowing() {
        renderTextPane();
    }

    public void componentHidden() {
    }

    public void componentActivated() {
    }

    public void componentDeactivated() {
    }

    public UndoRedo getUndoRedo() {
        return null;
    }

    public void setMultiViewCallback(MultiViewElementCallback callback) {
    }

    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;
    }
}
